package org.itec.yeast.trdo;

import java.util.ArrayList;
import java.util.List;

import org.itec.yeast.db.DataManager;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class TRCorrelationSQL {
	private static final Logger log = LoggerFactory.getLogger(TRCorrelationSQL.class);
	private static final String SQL_INSERT = "insert into correlation("
			+ "Strain1, Strain2, Correlation_Score, "
			+ "Correlation_Type, Correlation_Condition) "
			+ "values (?, ?, ?, ?, ?) ";
	
	
	private Object[] getParams(TRCorrelation correlation){
		Object[] params = new Object[]{
				correlation.getqStrain(),
				correlation.getaStrain(),
				correlation.getScore(),
				correlation.getType(),
				correlation.getCondition()
			};
		
		return params;
	}
	
	public void insert(TRCorrelation correlation){
		Object[] params = getParams(correlation);
		int result = DataManager.executeUpdate(SQL_INSERT, params);
		if (result != 0){
			log.info("插入Correlation数据成功");
		} else {
			log.info("未插入Correlation数据");
		}
	}

	public void insert(List<TRCorrelation> correlations){
		int result = -1;
		List<Object[]> list = new ArrayList<Object[]>();
		long start = System.currentTimeMillis();
		for (int i = 0; i < correlations.size(); i++){
			TRCorrelation correlation = correlations.get(i);
			Object[] params = getParams(correlation);
			list.add(params);
		}
		
		result = DataManager.executeBatchInsert(SQL_INSERT, list);
		long end = System.currentTimeMillis();
		log.info("插入" + result + "条Correlation记录，共用" + (end - start) + "ms");
	}
}
